'''
Created on 2009-6-12

@author: roamer
'''

import re

_Dic_StructParams = {
                 'H':((0.0,0.8,0.0,1.0),(0.2,1.0,0.0,1.0)),
                 'J':((0.0,1.0,0.0,0.8),(0.0,1.0,0.2,1.0)),
                 'L':((0.0,1.0,0.0,1.0),(0.1,0.9,0.1,0.9)),
                 'G':((0.0,1.0,0.0,1.0),),
                 'K':((0.0,1.0,0.0,0.6),(0.0,1.0,0.2,0.8),(0.0,1.0,0.4,1.0)),
                 'U':((0.0,1.0,0.0,0.4),(0.0,1.0,0.2,0.6),(0.0,1.0,0.4,0.8),(0.0,1.0,0.6,1.0)),
                 'V':((0.0,1.0,0.0,0.5),(0.0,1.0,0.2,0.6),(0.0,1.0,0.4,0.8),(0.0,1.0,0.5,0.9),(0.0,1.0,0.6,1.0)),
                 'I':((0.0,0.6,0.0,1.0),(0.2,0.8,0.0,1.0),(0.4,1.0,0.0,1.0)),
                 'X':((0.0,0.4,0.0,1.0),(0.2,0.6,0.0,1.0),(0.4,0.8,0.0,1.0),(0.6,1.0,0.0,1.0)),
                 'M':((0.0,1.0,0.0,1.0),(0.1,0.9,0.1,1.0)),
                 'O':((0.0,1.0,0.0,1.0),(0.1,1.0,0.1,1.0)),
                 'P':((0.0,1.0,0.0,1.0),(0.1,1.0,0.0,0.9)),
                 'Q':((0.0,1.0,0.0,1.0),(0.1,1.0,0.1,0.9)),
                 'R':((0.0,1.0,0.0,1.0),(0.0,0.9,0.1,1.0)),
                 'N':((0.0,1.0,0.0,1.0),(0.1,0.9,0.0,0.9)),
                 'S':((0.0,1.0,0.0,0.6),(0.0,0.6,0.4,1.0),(0.4,1.0,0.4,1.0)),
                 'T':((0.0,0.6,0.0,0.6),(0.4,1.0,0.0,0.6),(0.0,0.6,0.4,1.0),(0.4,1.0,0.4,1.0)),
                 'W':((0.0,0.6,0.0,0.6),(0.4,1.0,0.0,0.6),(0.0,0.6,0.4,1.0),(0.4,1.0,0.4,1.0))
                }
class ParseCode:
    def __init__(self,code):
        self.__code = code
    def UnitCodeCount(self):
        codes = self.__code.split(',')
        return len(filter(lambda x: len(x) == 3, codes))
    def CodeValid(self):
        if not self.__check():
            return False    
        ret,codes = self.__parse()
        return len(codes) == 1
    def __parse(self):       
        codes = self.__code.split(',')
        ret = []
        ret,codes = self.__doParse(0, (0.0,1.0,0.0,1.0), ret, codes)
        return ret,codes
    def __check(self):
        codes = self.__code.split(',')
        for i in codes:
            if len(i) == 1:
                if not i.isupper():
                    return False
            elif len(i) == 3:
                if re.match('[a-z0-9]{3}',i) is None:
                    return False
            else:
                return False
        return True
                
    def Parse(self):
        if not self.__check():
            return False
        ret,codes = self.__parse()
        if(len(codes) == 1):
            return ret
        else:
            return False
        
    def __doParse(self,index,zone,ret,codes):
        param = _Dic_StructParams[codes[index]]
        for i in xrange(0,len(param)):
            x = index + i + 1
            if(x >= len(codes)):
                break
            zone_i = param[i]
            x_start = zone[0] + zone_i[0] * (zone[1] - zone[0])
            x_end = zone[0] + zone_i[1] * (zone[1] - zone[0])
            y_start = zone[2] + zone_i[2] * (zone[3] - zone[2])
            y_end = zone[2] + zone_i[3] * (zone[3] - zone[2])
            newZone = (x_start,x_end,y_start,y_end)
            
            if(len(codes[x]) == 1):
                ret,codes = self.__doParse(x, newZone, ret, codes)
            else:
                x1 = newZone[0] * 32.0
                y1 = newZone[2] * 32.0
                width1 = 32.0 * (newZone[1] - newZone[0])
                height1 = 32.0 * (newZone[3] - newZone[2])
                ret.append((codes[x],(x1,y1,width1,height1)))
        for i in xrange(0,len(param)):
            del codes[index + 1]
        return ret,codes
